package ncme.auto.framework.core.mapper.methods;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import ncme.auto.framework.core.mapper.enums.CustomSqlMethod;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

public class SelectSingleField extends AbstractMethod {

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        CustomSqlMethod sqlMethod = CustomSqlMethod.SELECT_SINGLE_FIELD;

        String sql = String.format(
                sqlMethod.getSql(),
                sqlFirst(),
                sqlSelectColumns(tableInfo, true),
                tableInfo.getTableName(),
                sqlWhereEntityWrapper(true, tableInfo),
                sqlComment());

        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);// 进行预编译得到sqlSource对象

        //将预编译sql和其它的select相关的编译后的sql存在一起
        String methodName="selectSingleField";//Mapper中需要添加的方法名
        return addSelectMappedStatementForTable(mapperClass,methodName,sqlSource,tableInfo);
    }
}
